<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>react-demo-16</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link href="../../../js/font.css" rel="stylesheet" />
  </head>
  <style lang="scss">
    * {
      font-family: "Noto Sans SC", sans-serif;
    }

    body {
      display: flex;
      justify-content: flex-end;
    }

    .title {
      display: inline-flex;
      border: 1px solid blue;
      border-radius: 3px;
    }
  </style>
  <body>
    <!-- 准备容器 -->
    <div id="main"></div>

    <!-- 引入react核心库 -->
    <script
      type="text/javascript"
      src="../../../js/react.development.min.js"
    ></script>
    <!-- 引入react-dom，用于操作dom -->
    <script
      type="text/javascript"
      src="../../../js/react-dom.development.min.js"
    ></script>

    <!-- 引入prop-types -->
    <script src="../../../js/prop-types.js"></script>

    <!-- 引入babel，用于编译jsx -->
    <script src="../../../js/babel.min.js"></script>

    <!-- 此处一定要写babel，用于提示babel对该内容进行编译 -->
    <script type="text/babel">
      class Demo extends React.Component {
        state = {
          date: new Date().toLocaleTimeString(),
        };

        alertData = () => {
          const { input1 } = this;
          alert(input1.value);
        };

        timeUpdate = () => {
          this.setState({
            date: new Date().toLocaleTimeString(),
          });
        };

        // 作为Class的绑定函数调用则不会遭到ref的更新频繁调用
        saveInput = (ele) => {
          this.input1 = ele;
          console.log("@ class", ele);
        };

        render() {
          const { date } = this.state;
          // 内联回调ref，再更新实例时会调用两次函数，先清空，再重新指定
          // return (
          //   <div>
          //     <h2>{date}</h2>
          //     <button onClick={this.timeUpdate}>Change</button>
          //     <input
          //       ref={(ele) => {
          //         this.input1 = ele;
          //         console.log("@", ele);
          //       }}
          //       type="text"
          //     />

          //     <button onClick={this.alertData}>Alert</button>
          //   </div>
          // );

          return (
            <div>
              <h2>{date}</h2>
              <button onClick={this.timeUpdate}>Change</button>
              <input ref={this.saveInput} type="text" />
              <button onClick={this.alertData}>Alert</button>
            </div>
          );
        }
      }
      ReactDOM.render(<Demo />, document.getElementById("main"));
    </script>
  </body>
</html>
